<?php
namespace App\Repositories\Api;

use App\Models\Jump;
use App\Models\OfficialAccountSubscribe;
use Cache;

class JumpRepository
{

    public function getPages($filter = [], int $pageSize = 10, int $pageNum=1)
    {
        $str = join("-", array_values($filter)) ."-". $pageSize ."-". $pageNum;
        $cache_key = "cache_jump_" . md5($str);
        $res = Cache::get($cache_key);
        if(env("OPEN_CACHE") && $res) return $res;

        $res = Jump::where(["status" => $filter['status'] == 2 ? 0 : 1 ])
            ->when(!empty($filter['jump_key']), function ($query) use ($filter) {
              return $query->whereIn("jump_key", $filter['jump_key']);
            })
            ->orderBy('sort','desc')
            // ->paginate($pageSize, "*", null, $pageNum)
            ->get()
            ->toArray();
        // exit();

        Cache::set($cache_key, $res);
        return $res;
    }

    /**
     * 获取公众号关注状态
     * @Author   thomashtao               bosslen
     * @DateTime 2022-10-05T15:41:43+0800
     * @param    [type]                   $filter [description]
     * @return   [type]                           [description]
     */
    public function getJumpsSubscribe($filter)
    {
        $res = Jump::query()
            ->when(!empty($filter['id']), function ($query) use ($filter) {
              return $query->whereIn("jump.id", $filter['id']);
            })
            ->leftJoin("official_account_subscribe as oa", function($join) use ($filter){
                $join->on("oa.offiaccount", "=", "jump.id")
                    ->where("oa.unionid", "=", $filter['unionid']);
            })
            ->orderBy('sort','asc')
            ->get(["jump.id", "jump.jump_key", "jump.title", "jump.desc1","jump.m_summary","jump.f_summary", "jump.jump_mini_url","jump.logo_url","jump.sign_desc", "jump.m_path","jump.f_path","oa.is_subscribe"]);
            // ->toArray();

        return $res;
    }

    public function getEditorWeixin($push_id='')
    {
        $res = Jump::where(['id'=>$push_id])->first();
        if(empty($res['f_editor_qrcode'])){
            return '';
        }
        $editor_name = str_replace('.jpg', '', substr($res['m_editor_qrcode'],strripos($res['m_editor_qrcode'],"/")+1)) ;
        if (in_array($push_id, [1,3])) {
            return '<a href="'.env("IMG_DOMAIN") .$res['m_editor_qrcode'].'">男生点击添加小编微信发送2申请公众号发帖，发送4进脱单群'.$editor_name.'</a>'."\r\n\r\n\r\n".
                   '<a href="'.env("IMG_DOMAIN") .$res['f_editor_qrcode'].'">女生点击添加小编微信信发送2申请公众号发帖，发送4进脱单群'.$editor_name.'</a>';
        }else{
            return '<a href="'.env("IMG_DOMAIN") .$res['f_editor_qrcode'].'">点击添加小编微信发送2申请公众号发帖，发送4进脱单群'.$editor_name.'</a>';
        }
    }

}